﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TracNghiem.Areas.Admin.Models;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TracNghiem.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin")]
    public class ReportController : Controller
    {
        //
        // GET: /Admin/Report/
        BaiThiModel BaiThiModel = new BaiThiModel();
        DichvuModel DichvuModel = new DichvuModel();
        TestModel TestModel = new TestModel();

        public static List<BaiThiModel> bt_global { get; set; }

        public ActionResult Index()
        {
            //List<DichvuModel> DichvuModel_ = DichvuModel.getDichvu();
            //ViewBag.dichvu_id = new SelectList(DichvuModel_, "id", "name");

            List<BaiThiModel> bt = BaiThiModel.getReport_General();
            bt_global = bt;
            ViewBag.theanh = TracNghiem.Models.General.Get_select(null,"Tổng hợp DV");
            return View(bt);
        }
        BaiThi_CTModel BaiThi_CTModel = new BaiThi_CTModel();
        public ActionResult Detail(string user_id,string lan_thi,string de_thi_id,string user_name,string ho_ten,string don_vi,string ngay_thi)
        {
            ViewBag.user_name = user_name;
            ViewBag.ho_ten = ho_ten;
            ViewBag.don_vi = don_vi;
            ViewBag.ngay_thi = ngay_thi;
            List<BaiThi_CTModel> bt = BaiThi_CTModel.getKQ_CT(user_id, lan_thi, de_thi_id);
            return View(bt);
        }
        [HttpPost]
        public JsonResult getDVJson(string dichvu_id,string Order)
        {
            return Json(TestModel.getTestJson(dichvu_id));
        }
        public void Export_Excel()
        {
            var item = new System.Data.DataTable();
            item.Columns.Add("Trung tâm", typeof(string));
            item.Columns.Add("Tên đăng nhập", typeof(string));
            item.Columns.Add("Họ tên", typeof(string));
            item.Columns.Add("Đề thi", typeof(string));
            item.Columns.Add("Ngày thi", typeof(string));
            item.Columns.Add("Lần thi", typeof(string));
            item.Columns.Add("Câu đúng", typeof(string));
            item.Columns.Add("Tổng số câu", typeof(string));
            for (int i = 0; i < bt_global.ToArray().Length; i++)
                item.Rows.Add(bt_global[i].don_vi, bt_global[i].user_name, bt_global[i].ho_ten, bt_global[i].ten_de_thi, bt_global[i].ngay_thi, bt_global[i].lan_thi, bt_global[i].cau_dung,bt_global[i].tong_so_cau);
            var grid = new GridView();
            grid.DataSource = item;
            grid.DataBind();

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=bao_cao.xls");
            Response.ContentType = "application/ms-excel";
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            //Response.ContentType = "application/openxmlformats";
            //Response.AddHeader("Content-Disposition", "attachment; filename="+"bao_cao.xlsx");
            

            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
        [HttpPost]
        public ActionResult FindReport(string timkiem)
        {
            string dichvu_id = "";
            string de_thi_id = "";
            int num;
            string s = "";
            ViewBag.selectedService = "";
            ViewBag.selectedDethi = "";
            if (int.TryParse(Request.Form["service_id"], out num))
            {
                dichvu_id = Request.Form["service_id"];
                s = " and b.dichvu_id=" + dichvu_id;
                ViewBag.selectedService = Request.Form["service_id"];
                if (dichvu_id == "-1")
                    s = "and b.dichvu_id is null";
            }
            if (int.TryParse(Request.Form["de_thi_id"], out num))
            {
                de_thi_id = Request.Form["de_thi_id"];
                s = " and a.de_thi_id=" + de_thi_id;
                ViewBag.selectedDethi = Request.Form["de_thi_id"];
            }
            if (int.TryParse(Request.Form["service_id"], out num) && int.TryParse(Request.Form["de_thi_id"], out num))
                if (dichvu_id != "-1")
                    s = " and a.de_thi_id=" + de_thi_id + " AND b.dichvu_id=" + dichvu_id;
                else
                    s = " and a.de_thi_id=" + de_thi_id;
            List<BaiThiModel> Baithi = BaiThiModel.FindReport_General(s);
            List<DichvuModel> Dichvu = DichvuModel.getDichvu();
            if (ViewBag.selectedService.Length == 0)
                ViewBag.dichvu_id = new SelectList(Dichvu, "id", "name");
            else
            {
                ViewBag.dichvu_id = new SelectList(Dichvu, "id", "name", ViewBag.selectedService);
                List<TestModel> Dethi = TestModel.getTestByDV_ID(Convert.ToInt32(ViewBag.selectedService));
                ViewBag.test_id = new SelectList(Dethi, "id", "name", ViewBag.selectedDethi);
            }
            ViewBag.theanh = TracNghiem.Models.General.Get_select(Request.Form["service_id"],"Tổng hợp DV");
            bt_global = Baithi;
            return View("Index", Baithi);
        }
        
    }
}
